library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.3.3
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(tidyr)
library(lubridate)

Attaching package: 'lubridate'
The following objects are masked from 'package:base':

    date, intersect, setdiff, union
library(stringr)
library(emojifont)
Warning: package 'emojifont' was built under R version 4.3.3
library(ggtext)
Warning: package 'ggtext' was built under R version 4.3.3
library(leaflet)
library(sf)
Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats 1.0.0     ✔ readr   2.1.5
✔ purrr   1.0.2     ✔ tibble  3.2.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readr)
library(jsonlite)

Attaching package: 'jsonlite'

The following object is masked from 'package:purrr':

    flatten
library(wordcloud)
Warning: package 'wordcloud' was built under R version 4.3.3
Loading required package: RColorBrewer
library(tidytext)
airbnb_data <- read_csv("airbnb_data.csv")
Rows: 225 Columns: 50
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (16): Name, Title, Listing URL, Price, Qualifier, Can Instant Book, Orig...
dbl (10): Id, Beds, Baths, Bedrooms, Average Rating, Number of Reviews, Clea...
lgl (24): City, Cleaning Fee, Airbnb Service Fee, Total Charged for Stay, Ea...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
rats <- read_csv("Rat_Sightings_20240507.csv")|>
  filter(Latitude!="NA",
         Longitude!="NA")
Rows: 241846 Columns: 38
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (26): Created Date, Closed Date, Agency, Agency Name, Complaint Type, De...
dbl  (5): Unique Key, X Coordinate (State Plane), Y Coordinate (State Plane)...
lgl  (7): Vehicle Type, Taxi Company Borough, Taxi Pick Up Location, Bridge ...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
parks <- read_csv("Parks_Properties_20240507.csv")
Rows: 2047 Columns: 35
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (20): ADDRESS, BOROUGH, CLASS, DEPARTMENT, EAPPLY, GISPROPNUM, GlobalID...
dbl  (10): ACRES, COMMUNITYBOARD, COUNCILDISTRICT, GISOBJID, NYS_ASSEMBLY, N...
lgl   (4): PERMIT, PIP_RATABLE, RETIRED, WATERFRONT
dttm  (1): ACQUISITIONDATE

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
parks_sf <- st_as_sf(parks, wkt = "multipolygon")


url<- "https://data.cityofnewyork.us/resource/fi97-k4k6.json"

farmers_markets <- fromJSON(url)|>
  mutate(latitude=as.numeric(latitude),
         longitude=as.numeric(longitude))

boroughs<- "https://services5.arcgis.com/GfwWNkhOj9bNBqoJ/arcgis/rest/services/NYC_Borough_Boundary/FeatureServer/0/query?outFields=*&where=1%3D1&f=geojson"
boroughs<-fromJSON(boroughs)
rats_location <- rats |>
  select(Latitude, Longitude) 

rats_location
# A tibble: 239,532 × 2
   Latitude Longitude
      <dbl>     <dbl>
 1     40.7     -73.9
 2     40.8     -73.9
 3     40.8     -73.9
 4     40.8     -74.0
 5     40.7     -74.0
 6     40.8     -73.9
 7     40.7     -74.0
 8     40.6     -74.0
 9     40.8     -74.0
10     40.7     -74.0
# ℹ 239,522 more rows
rats_graph<-rats_location|>
  sample_n(225)  
airbnb_location<-airbnb_data|>
  select(Latitude, Longitude)
house_icon <- makeIcon(
  iconUrl = "https://www.iconeasy.com/icon/png/System/Vista%20General/house.png",
  iconWidth = 29,
  iconHeight = 29
)

rat_icon <- makeIcon(
  iconUrl = "https://icons.iconarchive.com/icons/google/noto-emoji-animals-nature/256/22251-rat-icon.png",
  iconWidth = 28,
  iconHeight = 28
)

leaflet() |>
  addTiles() |>
  setView(lng = -74.00, lat = 40.71, zoom = 12) |>
  addMarkers(data = airbnb_data, lng = ~Longitude, lat = ~Latitude, icon = house_icon,
     popup = ~paste("<a href='", `Listing URL`, "'><b>", Title, "</b></a>", "</br>", Name, "</br>", Price, Qualifier))|>
  addMarkers(data=rats_graph, lng=~Longitude, lat=~Latitude, icon = rat_icon, popup = ~paste("squeak squeak:)"))
carrot_icon <- makeIcon(
  iconUrl = "https://images.emojiterra.com/google/noto-emoji/unicode-15.1/color/svg/1f955.svg",
  iconWidth = 29,
  iconHeight = 29
)

leaflet() |>
  addTiles() |>
  setView(lng = -74.00, lat = 40.71, zoom = 12)|>
  addMarkers(data=farmers_markets, lng=~longitude, lat=~latitude, icon = carrot_icon, popup = ~paste(marketname, "</br>", daysoperation, hoursoperations))
leaflet() |>
  addTiles() |>
  setView(lng = -74.00, lat = 40.71, zoom = 12)|>
  addPolygons(data=parks_sf, color="deeppink", popup = ~paste(SIGNNAME))
word <- airbnb_data |>
  select(Name) |>
  mutate(Name = str_to_lower(Name),
         word = str_split(Name, "\\s+"))|>
         unnest(word) |>
  mutate(word = str_remove_all(word, "[^[:alpha:]]")) |>
  anti_join(stop_words) |>
  count(word, sort=TRUE)|>
  slice(2:n)
Joining with `by = join_by(word)`
Warning in 2:n: numerical expression has 378 elements: only the first used
pinkalicious <- c("#FF6FFF", "#FF5D99", "hotpink", "#FFCFF7", "deeppink")


wordcloud(words = word$word, freq = word$n, min.freq = 1,
          max.words = 100, random.order = FALSE, rot.per = 0.35, 
          colors = pinkalicious)

manhattan_lat_range <- c(40.70, 40.88)
manhattan_lon_range <- c(-74.02, -73.93)

brooklyn_lat_range <- c(40.57, 40.74)
brooklyn_lon_range <- c(-74.04, -73.82)

queens_lat_range <- c(40.53, 40.79)
queens_lon_range <- c(-73.96, -73.71)

bronx_lat_range <- c(40.79, 40.92)
bronx_lon_range <- c(-73.93, -73.73)

staten_island_lat_range <- c(40.48, 40.65)
staten_island_lon_range <- c(-74.27, -74.04)


airbnb_with_boroughs<-airbnb_data|>
  mutate(borough = ifelse(airbnb_data$Latitude >= manhattan_lat_range[1] & airbnb_data$Latitude <= manhattan_lat_range[2] &
                                airbnb_data$Longitude >= manhattan_lon_range[1] & airbnb_data$Longitude <= manhattan_lon_range[2],
                              "Manhattan",
                              ifelse(airbnb_data$Latitude >= brooklyn_lat_range[1] & airbnb_data$Latitude <= brooklyn_lat_range[2] &
                                       airbnb_data$Longitude >= brooklyn_lon_range[1] & airbnb_data$Longitude <= brooklyn_lon_range[2],
                                     "Brooklyn",
                                     ifelse(airbnb_data$Latitude >= queens_lat_range[1] & airbnb_data$Latitude <= queens_lat_range[2] &
                                              airbnb_data$Longitude >= queens_lon_range[1] & airbnb_data$Longitude <= queens_lon_range[2],
                                            "Queens",
                                            ifelse(airbnb_data$Latitude >= bronx_lat_range[1] & airbnb_data$Latitude <= bronx_lat_range[2] &
                                                     airbnb_data$Longitude >= bronx_lon_range[1] & airbnb_data$Longitude <= bronx_lon_range[2],
                                                   "Bronx",
                                                   ifelse(airbnb_data$Latitude >= staten_island_lat_range[1] & airbnb_data$Latitude <= staten_island_lat_range[2] &
                                                            airbnb_data$Longitude >= staten_island_lon_range[1] & airbnb_data$Longitude <= staten_island_lon_range[2],
                                                      "Staten Island", "No Borough"))))))
airbnb_with_boroughs|>
  mutate(Price=parse_number(Price))|>
  group_by(borough)|>
  ggplot()+
  geom_boxplot(aes(y = borough, x = Price), color="deeppink") +
  theme_minimal() +
  labs(y = "Borough", x = "Price") 

#Replace Price with "Beds", "Baths", `Average Rating`,`Number of Reviews`, `Cleanliness Rating`, etc.
#Note that Staten Island only has one listing